import time

import websitebase


class btcchina(websitebase.websitebase):
    _url_ticker = "https://data.btcchina.com/data/ticker?market=%s"

    def __init__(self):
        websitebase.websitebase.__init__(self)

    def getlast(self, symbol="btccny"):
        if self._string_is_empty(symbol):
            print("btcchina.getlast:输入的参数不能为空")
            return
        jdata = self.ticker(symbol)
        return jdata['ticker']['last']

    # symbol:"btccny"/"ltccny"
    def ticker(self, symbol="btccny"):
        if self._string_is_empty(symbol):
            print("btcchina.ticker:输入的参数不能为空")
            return
        if symbol != "btccny" and symbol != "ltccny":
            print("btcchina.ticker:输入的参数不正确")
            return
        url = self._url_ticker % (str(symbol))
        return self._req_json(url)


if __name__ == "__main__":
    t = time.time()
    a = btcchina()
    print(a.getlast())
    # print(a.ticker())
    print(time.time() - t)



# url = "https://www.btcchina.cn/api/v1/ticker.do?symbol=btc_cny"
#
# re = urllib.request.urlopen(url)
# data = re.read().decode("utf-8")
# print(data)
# jdata = json.loads(data)
# print(jdata)
# print(jdata['ticker']['last'])
